<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="订单流水号" prop="no" label-width="100px">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入订单流水号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="就诊人姓名" prop="paitentName" label-width="100px">
        <el-input
          v-model="queryParams.paitentName"
          placeholder="请输入就诊人姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="服务人员姓名" prop="staffName" label-width="100px">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入服务人员姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择订单状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.PEIZHEN_ORDER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['peizhen:buiess-order:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="服务项目名称" align="center" prop="programName" width="110" />
      <el-table-column label="订单流水号" align="center" prop="no"  width="150"/>
      <el-table-column label="医院名称" align="center" prop="hospitalName"  width="180"/>
      <el-table-column label="科室名称" align="center" prop="sectionName" />
      <el-table-column label="就诊人" align="center" prop="paitentName" />
      <el-table-column label="服务人员" align="center" prop="staffName" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PEIZHEN_ORDER_STATUS" :value="scope.row.status " />
        </template>
      </el-table-column>
      <el-table-column
        label="订单支付时间"
        align="center"
        prop="payTime"
        :formatter="dateFormatter"
        width="180px"
      />

      <el-table-column label="原价" align="center" prop="totalPrice" >
        <template #default="scope"> ￥{{ parseFloat(scope.row.totalPrice / 100).toFixed(2) }} </template>
      </el-table-column>
      <el-table-column label="应付金额" align="center" prop="payPrice" >
        <template #default="scope"> ￥{{ parseFloat(scope.row.payPrice / 100).toFixed(2) }} </template>
      </el-table-column>
      <el-table-column label="使用的积分" align="center" prop="usePoint"  width="100"/>
      <el-table-column label="积分抵扣金额" align="center" prop="pointPrice"  width="100"/>
      <el-table-column
        label="订单完成时间"
        align="center"
        prop="finishTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="160px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm( scope.row.id)"
            v-hasPermi="['peizhen:buiess-order:query']"
          >
            查看详情
          </el-button>
<!--          进行中的订单才能指派-->
          <el-button
            v-if="scope.row.status==2"
            link
            type="success"
            @click="openStaffForm(scope.row)"
            v-hasPermi="['peizhen:buiess-order:update']"
          >
            指派服务人员
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <BuiessOrderForm ref="formRef" @success="getList" />
  <PassStaff ref="staffForm" @success="getList" />
</template>

<script setup lang="ts">
import {getIntDictOptions,DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { BuiessOrderApi, BuiessOrderVO } from '@/api/peizhen/buiessorder'
import BuiessOrderForm from './BuiessOrderForm.vue'
import PassStaff from "@/views/peizhen/buiessorder/PassStaff.vue";


/** 订单 列表 */
defineOptions({ name: 'BuiessOrder' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<BuiessOrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  no: undefined,
  paitentName: undefined,
  status: undefined,
  payStatus: undefined,
  refundPrice: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BuiessOrderApi.getBuiessOrderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (id?:number) => {
  formRef.value.open(id)
}

/**指派服务人员弹窗*/
const staffForm = ref(null)
const openStaffForm = (order?: BuiessOrderVO) => {
  staffForm.value.open(order)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BuiessOrderApi.deleteBuiessOrder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await BuiessOrderApi.exportBuiessOrder(queryParams)
    download.excel(data, '订单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
